#!/usr/bin/perl
use warnings;
use strict;

my @views = `cleartool lsview -s | grep $ENV{"LOGNAME"}`;

print "Avaiable views:\n";
print "="x20 . "\n";

for(my $i = 1; $i <= $#views+1; $i++){
  print "$i): $views[$i-1]";
}
print "="x20 . "\n";
print "Your choice: ";
my $choice = <STDIN>;

if(($choice =~ /^\d+$/)
  && ($choice >= 1)
  && ($choice <= $#views+1))
{
  my $view = $views[$choice-1];
  chomp($view);
  my @groups = split /\s/, `groups`;
  print "Avaiable groups:\n";
  print "="x20 . "\n";
  for(my $i = 1; $i <= $#groups+1; $i++){
    print "$i): $groups[$i-1]\n";
  }
  print "="x20 . "\n";
  print "Your choice: ";
  $choice = <STDIN>;
  if(($choice =~ /^\d+$/)
    && ($choice >= 1)
    && ($choice <= $#views+1))
  {
    my $group = $groups[$choice-1];
    my $cmd = "cleartool setview -exec \"newgrp $group\" $view";
    print "cleartool setview -exec newgrp $group $view\n";
    system($cmd);
  }else{
    print "Wrong choice!\n";
    exit;
  }
}else{
  print "Wrong choice!\n";
  exit;
}

